<template>
  <div>
    <el-input
      v-model="filterText"
      style="margin: 4px 1% 0;width: 98%;"
      placeholder="输入关键字进行搜索"
      size="small"
    />
    <el-tree
      ref="tree"
      style="overflow: auto;"
      show-checkbox
      default-expand-all
      node-key="id"
      :data="data"
      :props="defaultProps"
      :filter-node-method="filterNode"
      @check="handleNodeClick"
    />
  </div>
</template>

<script>
import departmentAPI from '@/api/system/departmentAPI'

export default {
  name: 'DepartTree',
  data() {
    return {
      data: [],
      filterText: '',
      defaultProps: {
        label: 'name',
        children: 'children'
      }
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val)
    }
  },
  created() {
    departmentAPI.querySysDepartmentTree({ status: 1 }).then(response => {
      this.data = response.data
    })
  },
  methods: {
    handleNodeClick(data) {
      this.$emit('currentDepart', this.$refs.tree.getCheckedKeys())
    },
    clearNodeSelected() {
      this.$refs.tree.setChecked(1, false, true)
    },
    filterNode(value, data) {
      if (!value) return true
      return data.name.indexOf(value) !== -1
    }
  }
}
</script>

<style scoped>

</style>
